package com.tdam2013.grupo08.network;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import com.tdam2013.grupo08.R;

public class ServiceConnection {
	
	private static final String SERVICE_URL = "http://maxihp.servebeer.com:8080/MessageSender/";
	
	private static String resultMessage;
	private static int result; 
	private static XmlResultHandler handler;
	private static ArrayList<ServerMessage> mensajesRecibidos;
	
	public static String getResultMessage() {
		return resultMessage;
	}

	public static int getResult() {
		return result;
	}
	
	public static ArrayList<ServerMessage> getMensajesRecibidos()
	{
		return mensajesRecibidos;
	}
	
	public static void sendRequest(String request)
	{
		try 
		{
			String[] response = doPost(request);
			
			result = Integer.parseInt(response[0]);
			resultMessage = response[1];
		}
		catch (Exception e) {
			result = XmlResultHandler.RESULT_ERROR;
			resultMessage = "";
		}
	}
	
	private static String[] getResponse(InputStream response)
    {
		SAXParserFactory factory = SAXParserFactory.newInstance();
        try
        {
        	SAXParser parser = factory.newSAXParser();
            handler = new XmlResultHandler();
            parser.parse(response, handler);
            
            mensajesRecibidos = handler.getMessages();
            return new String[]{String.valueOf(handler.getResult()),handler.getResultInfo()};
        }
        catch (Exception e)
        {
        	throw new RuntimeException(e);
        }
    }
	
	private static String[] doPost (String request) throws ClientProtocolException, IOException
    {
		HttpPost httppost = new HttpPost(SERVICE_URL);  
        StringEntity se = new StringEntity(request);
            
        se.setContentType("text/xml");  
           
        httppost.setEntity(se);  

        HttpParams httpParameters = new BasicHttpParams();
        // Set the timeout in milliseconds until a connection is established.
        // The default value is zero, that means the timeout is not used. 
        int timeoutConnection = 3000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        
        // Set the default socket timeout (SO_TIMEOUT) 
        // in milliseconds which is the timeout for waiting for data.
        int timeoutSocket = 5000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
        
        HttpClient httpclient = new DefaultHttpClient(httpParameters);
        BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient.execute(httppost);
           
        return getResponse(httpResponse.getEntity().getContent()); 

    }

	
	
}
